﻿namespace GamePlus.OpenAuth.V20
{
    using System;
    using System.Collections.Generic;
    using System.Runtime.CompilerServices;
    using System.Runtime.InteropServices;

    public class Client
    {
        // Fields
        private const string BASE_URL = "https://api.weibo.com/2/";

        // Methods
        public Client(OAuth oauth)
        {
            this.OAuth = oauth;
         //   this.API = new Interfaces(this);
        }

        internal string GetCommand(string command, IEnumerable<KeyValuePair<string, object>> parameters)
        {
            List<WeiboParameter> list = new List<WeiboParameter>();
            foreach (KeyValuePair<string, object> pair in parameters)
            {
                list.Add(new WeiboStringParameter(pair.Key, pair.Value));
            }
            return this.Http(command, RequestMethod.Get, false, list.ToArray());
        }

        internal string GetCommand(string command, params WeiboParameter[] parameters)
        {
            return this.Http(command, RequestMethod.Get, false, parameters);
        }

        private string Http(string command, RequestMethod method, bool multi, params WeiboParameter[] parameters)
        {
            string url = string.Empty;
            if (command.StartsWith("http://") || command.StartsWith("https://"))
            {
                url = command;
            }
            else
            {
                url = string.Format("{0}{1}.json", "https://api.weibo.com/2/", command);
            }
            return this.OAuth.Request(url, method, multi, parameters);
        }

        internal string PostCommand(string command, IEnumerable<KeyValuePair<string, object>> parameters)
        {
            List<WeiboParameter> list = new List<WeiboParameter>();
            foreach (KeyValuePair<string, object> pair in parameters)
            {
                list.Add(new WeiboStringParameter(pair.Key, pair.Value));
            }
            return this.PostCommand(command, false, list.ToArray());
        }

        internal string PostCommand(string command, params WeiboParameter[] parameters)
        {
            return this.PostCommand(command, false, parameters);
        }

        internal string PostCommand(string command, bool multi = true, params WeiboParameter[] parameters)
        {
            return this.Http(command, RequestMethod.Post, multi, parameters);
        }

        // Properties
     //   public Interfaces API { get; private set; }

        public OAuth OAuth { get; private set; }
    }

}

